﻿Public Class QuanLyThuBUS
    Shared Function GetAll(Optional MaUser As Integer = 0) As DataTable
        Return QuanLyThuDAO.GetAll(MaUser)        
    End Function
    Shared Function GetAllByMa(ByVal MaPhieuThu As Integer) As DataTable
        Return QuanLyThuDAO.GetAllByMa(MaPhieuThu)
    End Function
    Shared Function TrungBinh(ByVal MaUser As Integer) As Double
        Return QuanLyThuDAO.TrungBinh(MaUser)
    End Function
    Shared Function TongTien(ByVal MaUser As Integer) As DataTable
        Return QuanLyThuDAO.TongTien(MaUser)
    End Function
    Shared Function Write(obj As PhieuThu, Optional MaPhieuThu As Integer = 0) As Boolean
        If obj.MaUserThu.Equals(0) Then
            MessageBox.Show("Xin chọn người dùng")
            Return False
        End If
        If obj.MaNganHang.Equals(0) Then
            MessageBox.Show("Xin chọn tài khoản")
            Return False
        End If
        If obj.MaThuNhap.Equals(0) Then
            MessageBox.Show("Xin chọn loại thu nhập")
        End If
        Dim dr As DataRow
        Dim dt As DataTable
        If MaPhieuThu = 0 Then
            dt = DB_Provider.Read_schema("PhieuThu")
            dr = dt.NewRow()
            dr("Ngay") = obj.Ngay
            dr("MaNganHang") = obj.MaNganHang
            dr("MaUserThu") = obj.MaUserThu
            dr("MaUserLap") = obj.MaUserLap
            dr("SoTien") = obj.SoTien
            dr("MaThuNhap") = obj.MaThuNhap
            dr("ChiTiet") = obj.ChiTiet            
            dt.Rows.Add(dr)
        Else
            dt = GetAllByMa(MaPhieuThu)
            Dim pk(1) As DataColumn
            pk(0) = dt.Columns("MaPhieuThu")
            dt.PrimaryKey = pk
            dr = dt.Rows.Find(MaPhieuThu)
            dr("Ngay") = obj.Ngay
            dr("MaNganHang") = obj.MaNganHang
            dr("MaUserThu") = obj.MaUserThu
            dr("MaUserLap") = obj.MaUserLap
            dr("SoTien") = obj.SoTien
            dr("MaThuNhap") = obj.MaThuNhap
            dr("ChiTiet") = obj.ChiTiet
        End If
        DB_Provider.Write(dt, "PhieuThu")
        Dim MaNganHang As Integer = Integer.Parse(dt.Rows(0).Item("MaNganHang"))
        Dim SoTien As Double = Double.Parse(dt.Rows(0).Item("SoTien").ToString())
        TaiKhoanNganHangDAO.UpdateSoTien(MaNganHang, SoTien)
        Return True
    End Function
End Class
